﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static DataMonitor_Tools.Tools.CSVFileWatch;

namespace DataMonitor_Tools.Tools.SMTP
{
    public static class SMTPWarnContent
    {
        public static string CreateWarnContent(CSVFileWatchStruct cSVFileWatchStruct,string vehicle,int accerror,int diserror)
        {
            string errormessage = "";
            switch (cSVFileWatchStruct.status)
            {
                case 1:
                    errormessage += $"{vehicle}的{cSVFileWatchStruct.name}文件中出现了不是数字的问题\n";
                    break;
                case 2: 
                    errormessage += $"{vehicle}的{cSVFileWatchStruct.name}文件中大小为0的问题\n";
                    break;
                default:
                    break;
            }
            foreach (string header in cSVFileWatchStruct.headers)
            {
                if (header.Contains("acc_"))
                {
                    var idx = cSVFileWatchStruct.headers.IndexOf(header);
                    var acc = cSVFileWatchStruct.alldatalist[idx].Any(a => a > accerror);
                    if (acc)
                    {
                        errormessage += $"{vehicle}的{cSVFileWatchStruct.name}文件中{header}通道出现了大于{accerror}的问题\n";
                        break;
                    }
                }
                if (header.Contains("dis_"))
                {
                    var idx = cSVFileWatchStruct.headers.IndexOf(header);
                    var acc = cSVFileWatchStruct.alldatalist[idx].Any(a => a > diserror);
                    if (acc)
                    {
                        errormessage += $"{vehicle}的{cSVFileWatchStruct.name}文件中{header}通道出现了大于{diserror}的问题\n";
                        break;
                    }
                }
            }
            return errormessage;
        }
        /// <summary>
        /// 识别日志里是否有错误信息并返回
        /// </summary>
        /// <param name="contents">日志信息</param>
        /// <param name="keywords">错误关键词</param>
        /// <param name="logfilename">日志文件名</param>
        /// <returns></returns>
        public static string CreateWarnContentforLog(string[] contents, string[] keywords, int can0, int idcount,string logfilename)
        {
            string errormessage = "";
            var matchingElements = contents.SelectMany(s => keywords.Where(k => s.Contains(k)).Select(k => new { Element = s, Keyword = k }));
            if(matchingElements.Count() > 0 )
            {
                foreach (var match in matchingElements)
                {
                    errormessage+=($"{logfilename}的元素 '{match.Element}' 包含关键字 '{match.Keyword}'。")+Environment.NewLine;
                }
            }
            string pattern1 = @"data:\s(\d+)";
            string pattern2 = @"can0:\s(\d+)";
            Regex regex1 = new Regex(pattern1);
            foreach (string content in contents)
            {
                Match match1 = regex1.Match(content);
                if (match1.Success)
                {
                    string data1 = match1.Groups[1].Value;
                    bool su=int.TryParse(data1, out int r);
                    if (su)
                    {
                        if (r < idcount)
                        {
                            errormessage += ($"{logfilename} 的id数量 {r} 小于标准值 {idcount}。") + Environment.NewLine;
                            break;
                        }
                    }
                    
                }
            }
            Regex regex2 = new Regex(pattern2);
            foreach (string content in contents)
            {
                Match match2 = regex2.Match(content);
                if (match2.Success)
                {
                    string data2 = match2.Groups[1].Value;
                    bool su = int.TryParse(data2, out int r);
                    if (su)
                    {
                        if (r < can0)
                        {
                            errormessage += ($"{logfilename} 的can0数据量 {r} 小于标准值 {can0}。") + Environment.NewLine;
                            break;
                        }
                    }

                }
            }
            return errormessage;
        }
        public static bool IsPastOneHour(DateTime startTime,int timespan)
        {
            // 获取当前时间
            DateTime currentTime = DateTime.Now;

            // 计算时间间隔
            TimeSpan timeElapsed = currentTime - startTime;

            // 判断是否过了1小时
            if (timeElapsed.TotalMinutes >= timespan)
            {
                return true;
            }

            return false;
        }

    }
}
